﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import flash.net.*;
    import flash.external.*;

    public class Utilities {

        private static var m_idCounter:Number = 0;

        public static function removeAllChildren(_arg1:DisplayObjectContainer):void{
            var _local2:int;
            if (_arg1 != null){
                _local2 = _arg1.numChildren;
                while (_local2 > 0) {
                    _arg1.removeChildAt((_local2 - 1));
                    _local2--;
                };
            };
        }
        public static function clamp(_arg1:Number, _arg2:Number, _arg3:Number):Number{
            var _local4:Number = Math.max(_arg1, _arg2);
            _local4 = Math.min(_local4, _arg3);
            return (_local4);
        }
        public static function getStackTrace():String{
            var stackTraceLines:* = null;
            var lineToParse:* = null;
            var stackTrace:* = null;
            var result:* = "";
            var tempError:* = new Error();
            try {
                stackTrace = tempError.getStackTrace();
            } catch(e:Error) {
            };
            if (((!((stackTrace == null))) && (!((stackTrace == ""))))){
                stackTraceLines = stackTrace.split("\n", 5);
                lineToParse = String(stackTraceLines[4]);
                result = lineToParse.substring(4, (lineToParse.indexOf("()", 4) + 2));
            };
            return (result);
        }
        public static function hexColorToIntColor(_arg1:String):uint{
            var _local2:uint;
            if (_arg1.length == 7){
                _local2 = parseInt(("0x" + _arg1.substr(1)));
            } else {
                if (_arg1.length == 6){
                    _local2 = parseInt(("0x" + _arg1));
                };
            };
            return (_local2);
        }
        public static function getSmoothableBitmap(_arg1:DisplayObject):Bitmap{
            var _local2:BitmapData = new BitmapData(_arg1.width, _arg1.height, true, 0);
            _local2.draw(_arg1, null, null, null, null, true);
            var _local3:Bitmap = new Bitmap(_local2, "auto", true);
            return (_local3);
        }
        public static function intColorToHexColor(_arg1:uint):String{
            var _local2:String = _arg1.toString(16);
            while (_local2.length < 6) {
                _local2 = ("0" + _local2);
            };
            return (_local2.toUpperCase());
        }
        public static function isURL(_arg1:String):Boolean{
            var _local2:RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
            return (_local2.test(_arg1));
        }
        public static function launchURL(_arg1:String, _arg2:String="_blank", _arg3:Object=null):void{
            var browser:* = null;
            var url:* = _arg1;
            var target:String = _arg2;
            var variables = _arg3;
            var request:* = new URLRequest(url);
            var useNavigateToURL:* = true;
            if (variables){
                request.data = variables;
            };
            try {
                if (ExternalInterface.available){
                    browser = ExternalInterface.call("function a() {return navigator.userAgent;}");
                    if (((!((browser == null))) && ((browser.indexOf("Firefox") < 1)))){
                        useNavigateToURL = true;
                    } else {
                        ExternalInterface.call("window.open", url, target, "");
                        useNavigateToURL = false;
                    };
                } else {
                    useNavigateToURL = true;
                };
            } catch(error:SecurityError) {
                useNavigateToURL = true;
            };
            if (useNavigateToURL){
                navigateToURL(request, target);
            };
        }
        public static function centerWindow(_arg1:DisplayObject):void{
            _arg1.x = ((GlobalEngine.stage.stageWidth - _arg1.width) / 2);
            _arg1.y = ((GlobalEngine.stage.stageHeight - _arg1.height) / 2);
        }
        public static function extractBasicTag(_arg1:String, _arg2:String):String{
            var _local5:int;
            var _local3:String;
            var _local4:int = _arg1.indexOf((_arg2 + "=\""));
            if (_local4 != -1){
                _local4 = (_local4 + (_arg2.length + 2));
                _local5 = _arg1.indexOf("\"", _local4);
                if (_local5 != -1){
                    _local3 = _arg1.substring(_local4, _local5);
                };
            };
            return (_local3);
        }
        public static function extractStyle(_arg1:String, _arg2:String):String{
            var _local5:int;
            _arg1 = (_arg1 + ";");
            var _local3:String;
            var _local4:int = _arg1.indexOf((_arg2 + ":"));
            if (_local4 != -1){
                _local4 = (_local4 + (_arg2.length + 1));
                _local5 = _arg1.indexOf(";", _local4);
                if (_local5 != -1){
                    _local3 = _arg1.substring(_local4, _local5);
                };
            };
            return (_local3);
        }
        public static function createSquareBitmapData(_arg1:DisplayObject):BitmapData{
            var _local2:Number = Math.min(_arg1.width, _arg1.height);
            var _local3:Point = new Point(((_arg1.width - _local2) / 2), ((_arg1.height - _local2) / 2));
            var _local4:Matrix = new Matrix();
            _local4.translate(-(_local3.x), -(_local3.y));
            var _local5:BitmapData = new BitmapData(_local2, _local2, true, 0);
            _local5.draw(_arg1, _local4);
            return (_local5);
        }
        public static function generateUniqueId():Number{
            m_idCounter++;
            return (m_idCounter);
        }
        public static function setFullScreen(_arg1:Boolean):void{
            var fullscreen:* = _arg1;
            if (GlobalEngine.stage){
                try {
                    if (fullscreen){
                        GlobalEngine.stage.displayState = StageDisplayState.FULL_SCREEN;
                    } else {
                        GlobalEngine.stage.displayState = StageDisplayState.NORMAL;
                    };
                } catch(err:SecurityError) {
                };
                GlobalEngine.log("Engine", ("Toggling Fullscreen, DisplayState: " + GlobalEngine.stage.displayState.toString()));
            };
        }
        public static function isFullScreen():Boolean{
            return ((GlobalEngine.stage.displayState == StageDisplayState.FULL_SCREEN));
        }
        public static function toggleFullScreen():void{
            setFullScreen((isFullScreen() == false));
        }
        private static function onFullScreenChanged(_arg1:FullScreenEvent):void{
            GlobalEngine.viewport.centerViewport();
        }
        public static function randBetween(_arg1:Number, _arg2:Number):Number{
            return (((Math.random() * (_arg2 - _arg1)) + _arg1));
        }
        public static function formatNumber(_arg1:int):String{
            var _local4:Number;
            var _local2:String = _arg1.toString();
            var _local3:Array = new Array();
            var _local5:Number = _local2.length;
            while (_local5 > 0) {
                _local4 = Math.max((_local5 - 3), 0);
                _local3.unshift(_local2.slice(_local4, _local5));
                _local5 = _local4;
            };
            var _local6:String = _local3.join(",");
            return (_local6);
        }
        public static function parseLocalizationString(_arg1:String):Object{
            var _local2:Array = _arg1.split(":");
            var _local3:Object = new Object();
            _local3.filename = _local2[0];
            _local3.stringname = _local2[1];
            return (_local3);
        }
        public static function dateToNumber(_arg1:String, _arg2:Boolean=false):Number{
            var _local4:Array;
            var _local3:Number = 0;
            if (_arg2){
                _local4 = String(_arg1).split("/", 3);
                _local3 = Date.UTC(parseInt(_local4[2]), (parseInt(_local4[0]) - 1), parseInt(_local4[1]));
            } else {
                _local3 = new Date(_arg1).valueOf();
            };
            return (_local3);
        }
        public static function xmlListToArray(_arg1:XMLList, _arg2:String=""):Array{
            var _local4:XML;
            var _local3:Array = new Array();
            if (_arg2 == ""){
                for each (_local4 in _arg1) {
                    _local3.push(_local4);
                };
            } else {
                for each (_local4 in _arg1) {
                    _local3.push(String(_local4.attribute(_arg2)));
                };
            };
            return (_local3);
        }
        public static function uncompress(_arg1:ByteArray):String{
            var compressedData:* = _arg1;
            var compressedLength:* = compressedData.length;
            try {
                compressedData.uncompress();
            } catch(e:Error) {
                throw (e);
            };
            GlobalEngine.msg("Loaded compressed localization file. Originally:", compressedLength, "Decompressed:", compressedData.length);
            return (compressedData.toString());
        }
        public static function printr(_arg1, _arg2:int=0, _arg3:String=""){
            var _local6:*;
            var _local7:String;
            if (_arg2 == 0){
                _arg3 = (("(" + Utilities.typeOf(_arg1)) + ") {\n");
            } else {
                if (_arg2 == 10){
                    return (_arg3);
                };
            };
            var _local4 = "\t";
            var _local5:int;
            while (_local5 < _arg2) {
                _local5++;
                _local4 = (_local4 + "\t");
            };
            for (_local6 in _arg1) {
                _arg3 = (_arg3 + (((((_local4 + "[") + _local6) + "] => (") + Utilities.typeOf(_arg1[_local6])) + ") "));
                if (Utilities.count(_arg1[_local6]) == 0){
                    _arg3 = (_arg3 + _arg1[_local6]);
                };
                _local7 = "";
                if (typeof(_arg1[_local6]) != "xml"){
                    _local7 = Utilities.printr(_arg1[_local6], (_arg2 + 1));
                };
                if (_local7 != ""){
                    _arg3 = (_arg3 + ((("{\n" + _local7) + _local4) + "}"));
                };
                _arg3 = (_arg3 + "\n");
            };
            if (_arg2 == 0){
                trace((_arg3 + "}\n"));
            } else {
                return (_arg3);
            };
        }
        public static function typeOf(_arg1):String{
            if ((_arg1 is Array)){
                return ("array");
            };
            if ((_arg1 is Date)){
                return ("date");
            };
            return (typeof(_arg1));
        }
        public static function count(_arg1:Object):uint{
            var _local2:uint;
            var _local3:*;
            if (Utilities.typeOf(_arg1) == "array"){
                return (_arg1.length);
            };
            _local2 = 0;
            for (_local3 in _arg1) {
                if (_local3 != "mx_internal_uid"){
                    _local2++;
                };
            };
            return (_local2);
        }
        public static function trim(_arg1:String):String{
            return (_arg1.replace(/^(\s| )+|(\s| )+$/g, ""));
        }
        public static function arrayRand(_arg1:Array):Object{
            return (_arg1[Math.floor((Math.random() * _arg1.length))]);
        }
        public static function arrayDiff(_arg1:Array, _arg2:Array):Array{
            var _local3:int;
            var _local6:Object;
            var _local4:Array = new Array();
            var _local5:Dictionary = new Dictionary();
            _local3 = 0;
            while (_local3 < _arg1.length) {
                _local5[_arg1[_local3]] = true;
                _local3++;
            };
            _local3 = 0;
            while (_local3 < _arg2.length) {
                if (_local5[_arg2[_local3]]){
                    delete _local5[_arg2[_local3]];
                } else {
                    _local4.push(_arg2[_local3]);
                };
                _local3++;
            };
            for (_local6 in _local5) {
                _local4.push(_local6);
            };
            return (_local4);
        }
        public static function arrayIntersect(_arg1:Array, _arg2:Array):Array{
            var _local3:int;
            var _local4:Array = new Array();
            var _local5:Dictionary = new Dictionary();
            _local3 = 0;
            while (_local3 < _arg1.length) {
                _local5[_arg1[_local3]] = true;
                _local3++;
            };
            _local3 = 0;
            while (_local3 < _arg2.length) {
                if (_local5[_arg2[_local3]]){
                    _local4.push(_arg2[_local3]);
                };
                _local3++;
            };
            return (_local4);
        }
        public static function splitAndTrim(_arg1:String, _arg2:String):Array{
            var _local3:Array = _arg2.split(_arg1);
            var _local4:int;
            var _local5:int = _local3.length;
            while (_local4 < _local5) {
                _local3[_local4] = trim((_local3[_local4] as String));
                _local4++;
            };
            return (_local3);
        }

    }
}//package Engine 
